package com.campus.controller;

import com.campus.dto.BatchAuditRequest;
import com.campus.dto.StudentUserDto;
import com.campus.model.Student;
import com.campus.model.User;
import com.campus.service.StudentService;

import com.campus.web.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/homework/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 单个添加学生
    @PostMapping("/add")
    public ResponseResult<String> addStudent(@RequestBody StudentUserDto studentUserDto) {
        Student student = studentUserDto.getStudent(); // 获取 student 对象
        User user = studentUserDto.getUser();          // 获取 user 对象

        boolean success = studentService.addStudentAndUser(student, user);

        return success
                ? ResponseResult.ok("Student and User added successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to add student and user");
    }

    // 批量上传学生
    @PostMapping("/batch-upload")
    public ResponseResult<String> batchUploadStudents(@RequestParam("file") MultipartFile file) {
        try {
            boolean success = studentService.batchAddStudentsWithUsersFromExcel(file);
            return success
                    ? ResponseResult.ok("Batch import successful.")
                    : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Batch import failed.");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(HttpStatus.BAD_REQUEST, "Invalid file format or error: " + e.getMessage());
        }
    }

    // 审核学员
    @PutMapping("/audit/{id}")
    public ResponseResult<String> auditStudent(@PathVariable Integer id, @RequestParam String status) {
        boolean success = studentService.updateStudentStatus(id, status);

        return success
                ? ResponseResult.ok("Student audited successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to audit student");
    }

    // 批量审核学员
    @PutMapping("/batchAudit")
    public ResponseResult<String> batchAuditStudents(@RequestBody BatchAuditRequest request) {
        List<Integer> studentIds = request.getStudentIds();
        String status = request.getStatus();

        if (studentIds == null || studentIds.isEmpty() || status == null) {
            return ResponseResult.error(HttpStatus.BAD_REQUEST, "Invalid request body");
        }

        studentService.batchUpdateStudentStatus(studentIds, status);
        return ResponseResult.ok("Batch audit successful");
    }

    // 编辑学生信息
    @PutMapping("/edit/{id}")
    public ResponseResult<String> editStudent(@PathVariable Integer id, @RequestBody Student updatedStudent) {
        boolean success = studentService.editStudent(id, updatedStudent);

        return success
                ? ResponseResult.ok("Student edited successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to edit student");
    }

    // 删除学生
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteStudent(@PathVariable Integer id) {
        boolean success = studentService.deleteStudent(id);

        return success
                ? ResponseResult.ok("Student deleted successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete student");
    }

    // 查询所有学生
    @GetMapping("/list")
    public ResponseResult<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();

        return students != null && !students.isEmpty()
                ? ResponseResult.ok(students)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No students found");
    }
}
